import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

class MyApp18 extends StatefulWidget {
  const MyApp18({super.key});

  @override
  State<MyApp18> createState() => _MyApp18State();
}

class _MyApp18State extends State<MyApp18> {
  //频道列表
  List _channels = ['推荐', '热点', '北京', '社会', '娱乐', '科技', '财经', '游戏', '订阅'];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getChannels();
  }

  _getChannels() async {
    String BASE_URL = "https://geek.itheima.net"; // 请求基础地址
    //1.1创建dio实例
    Dio dio = Dio();
    var res = await dio.get('$BASE_URL/v1_0/channels');
    // print(res);
    setState(() {
      _channels = res.data['data']['channels'] as List;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Theme.of(context).colorScheme.inversePrimary,
          //widget是获取上面的类的实例（获取父组件的属性）
          title: Text('dio体验'),
        ),
        body: GridView(
          gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
              childAspectRatio: 1.5,
              mainAxisSpacing: 10,
              crossAxisSpacing: 10,
              maxCrossAxisExtent: 200),
          children: _channels.map((item) {
            return Container(
              alignment: Alignment.center,
              color: Colors.blue,
              child: Text(item['name']),
            );
          }).toList(),
        ));
  }
}
